//
//  LSBaseTabBarController.m
//  LookScore
//
//  Created by wr on 2017/12/4.
//  Copyright © 2017年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSBaseTabBarController.h"
#import "LSNavigationViewController.h"
#import "LSHomeVController.h"         //主页
#import "LSCompetitionVController.h"  //赛事
#import "LSExponentVController.h"     //指数
#import "LSChatCircleVController.h"   //聊天圈
#import "WRNavigationBar.h"
@interface LSBaseTabBarController ()

@property(strong,nonatomic)NSMutableArray *childArray;

@property(assign,nonatomic)NSInteger seletedIndex;

@property(strong,nonatomic)NSMutableArray *tabBarButtonArray;
@end

@implementation LSBaseTabBarController{
    BOOL isOK;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    self.tabBar.tintColor = LSNavMainColor;
    
    self.tabBar.backgroundColor=[UIColor whiteColor];
    self.childArray=[NSMutableArray array];
    
    self.tabBarButtonArray=[NSMutableArray array];
    for (UIView *tabBarButton in self.tabBar.subviews){
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]){
            [self.tabBarButtonArray addObject:tabBarButton];
        }
    }
    
    BOOL config = [[NSUserDefaults standardUserDefaults] boolForKey:@"isOk"];
    [self initWithChildControllerWithConfig:config];
}

- (void)loadConfig{
//    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isOk"];
    [LSHttpEnclosureTool GET:LSConfig parameters:nil resBlock:^(id responseObject) {
        if (responseObject) {
            BOOL config;

            MyLog(@"Config -- %@",responseObject);
            if ([responseObject isEqual:@(0)]) {
                config = NO;
            }else{
                config = YES;
            }
//            [[NSUserDefaults standardUserDefaults] setBool:config forKey:@"isOk"];

            [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isOk"];


        }else{
            MyLog(@"error");
        }
    }];
}

- (void)initWithChildControllerWithConfig:(BOOL)config{
    
    //首页
    LSHomeVController *homeVC = [[LSHomeVController alloc] init];
    [homeVC.tabBarItem setTitle:@"首页"];
    [homeVC.tabBarItem setImage:[UIImage imageNamed:@"TabHome"]];
    [homeVC.tabBarItem setSelectedImage:[[UIImage imageNamed:@"TabHomeS"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    LSNavigationViewController  *homeNV = [[LSNavigationViewController alloc] initWithRootViewController:homeVC];
    [homeNV.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -3)];
    [self.childArray addObject:homeNV];
    
    //赛事
    LSCompetitionVController *competitionVC = [[LSCompetitionVController alloc] init];
    [competitionVC.tabBarItem setTitle:@"赛事"];
    [competitionVC.tabBarItem setImage:[UIImage imageNamed:@"TabScore"]];
    [competitionVC.tabBarItem setSelectedImage:[[UIImage imageNamed:@"TabScoreS"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    LSNavigationViewController *competitionNV = [[LSNavigationViewController alloc] initWithRootViewController:competitionVC];
    [competitionNV.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -3)];
    [self.childArray addObject:competitionNV];
    
    
    if (!config) {
        //指数
        LSExponentVController *exponentVC = [[LSExponentVController alloc] init];
        [exponentVC.tabBarItem setTitle:@"指数"];
        [exponentVC.tabBarItem setImage:[UIImage imageNamed:@"TabExponent"]];
        [exponentVC.tabBarItem setSelectedImage:[[UIImage imageNamed:@"TabExponentS"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        LSNavigationViewController *exponentNV= [[LSNavigationViewController alloc] initWithRootViewController:exponentVC];
        [exponentNV.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -3)];
        [self.childArray addObject:exponentNV];
    }
    
    
    //圈子
    LSChatCircleVController *cricleVC = [[LSChatCircleVController alloc] init];
    [cricleVC.tabBarItem setTitle:@"圈子"];
    [cricleVC.tabBarItem setImage:[UIImage imageNamed:@"TabChatCircle"]];
    [cricleVC.tabBarItem setSelectedImage:[[UIImage imageNamed:@"TabChatCircleS"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    LSNavigationViewController *cricleNV = [[LSNavigationViewController alloc] initWithRootViewController:cricleVC];
    [cricleNV.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -3)];
    [self.childArray addObject:cricleNV];
    
    [self setViewControllers:self.childArray];
    [self setNavBarAppearence];
 
}

- (void)setNavBarAppearence{

    // 设置是 广泛使用WRNavigationBar，还是局部使用WRNavigationBar，目前默认是广泛使用
    [WRNavigationBar wr_widely];
    // 设置导航栏默认的背景颜色
    [WRNavigationBar wr_setDefaultNavBarBarTintColor:LSNavMainColor];
    // 设置导航栏所有按钮的默认颜色
    [WRNavigationBar wr_setDefaultNavBarTintColor:[UIColor whiteColor]];
    // 设置导航栏标题默认颜色
    [WRNavigationBar wr_setDefaultNavBarTitleColor:LSNavTitlenColor];
    // 统一设置状态栏样式
    [WRNavigationBar wr_setDefaultStatusBarStyle:UIStatusBarStyleLightContent];
    // 如果需要设置导航栏底部分割线隐藏，可以在这里统一设置
    [WRNavigationBar wr_setDefaultNavBarShadowImageHidden:YES];
    [WRNavigationBar wr_setBlacklist:@[@"LSUserFeedbackVController"]];
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    
//    NSInteger index = [self.tabBar.items indexOfObject:item];
    
    //一个连续点击15下 红包领取的时候就可以直接显示金额和个数供测试时使用

    
//    if (index!=_seletedIndex) {
//        [self.tabBar.items enumerateObjectsUsingBlock:^(UITabBarItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            UITabBarItem *tempItem=(UITabBarItem *)obj;
//            if (obj ==tabBar.items[index]) {
//
//                [tempItem setTitlePositionAdjustment:UIOffsetMake(0, 100)];
//                [tempItem setImageInsets:UIEdgeInsetsMake(5, 0, -5, 0)];
//            }else{
//                [tempItem setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
//                [tempItem setTitlePositionAdjustment:UIOffsetMake(0, -3)];
//            }
//        }];
//        CABasicAnimation *pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
//        pulse.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
//        pulse.duration = 0.1;
//        pulse.repeatCount = 1;
//        pulse.autoreverses =NO;
//        pulse.fromValue = [NSNumber numberWithFloat:0.4];//开始时的倍率
//        pulse.toValue = [NSNumber numberWithFloat:0.8];//结束时的倍率
//        [[self.tabBarButtonArray[index] layer] addAnimation:pulse forKey:@"itemScaleKeyPath"];
//        _seletedIndex = index;
//
//    }
    
  
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
